import os
import pathlib

def rename_png_files(dir_path):
    count = 1
    for filename in os.listdir(dir_path):
       if filename.__contains__('.') and not filename.__contains__(".py"): 
            old_file_path = os.path.join(dir_path, filename)
            new_file_name = f"temp({count}).png"
            new_file_path = os.path.join(dir_path, new_file_name)

            os.rename(old_file_path, new_file_path)
            count += 1


    count = 1
    for filename in os.listdir(dir_path):
        if filename.__contains__('.') and not filename.__contains__(".py"): 
            old_file_path = os.path.join(dir_path, filename)
            new_file_name = f"P({count}).png"
            new_file_path = os.path.join(dir_path, new_file_name)

            # 检查新的文件名是否已存在，如果存在则跳过本次循环并增加计数器
            while os.path.exists(new_file_path):
                count += 1
                new_file_name = f"P({count}).png"
                new_file_path = os.path.join(dir_path, new_file_name)

            os.rename(old_file_path, new_file_path)
            count += 1

# 获取当前脚本所在目录的绝对路径
current_dir = pathlib.Path(__file__).resolve().parent

rename_png_files(current_dir)
print("All .png files in the current directory and its subdirectories have been renamed.")
